package com.objecteffects.ilmp.persist.jpa;

import java.util.List;

import javax.persistence.EntityManager;

import com.objecteffects.ilmp.model.BlogPostsOffsetEntity;
import com.objecteffects.ilmp.model.TumblrBlogEntity;
import com.objecteffects.ilmp.model.UserEntity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Rusty Wright
 */
public final class RemovePrevious {
    private RemovePrevious() {
        //
    }

    private static final Logger LOG = LoggerFactory
        .getLogger(RemovePrevious.class);

    /* remove previous stuff */
    public static void removePrevious(final EntityManager em) {
        // em.getTransaction().begin();
        //
        // @SuppressWarnings("unchecked")
        // final List<UserCookieEntity> cookies =
        // em.createQuery("from " + UserCookieEntity.class.getName())
        // .getResultList();
        //
        // for (final UserCookieEntity each : cookies) {
        // RemovePrevious.log.debug("deleting cookie: {}", each);
        //
        // em.remove(each);
        // }
        //
        // em.getTransaction().commit();
        //
        // RemovePrevious.log.debug("deleted {} cookies",
        // Integer.valueOf(cookies.size()));

        em.getTransaction().begin();

        @SuppressWarnings("unchecked")
        final List<UserEntity> users =
        em.createQuery("from " + UserEntity.class.getName())
        .getResultList();

        for (final UserEntity eachUser : users) {
            for (final TumblrBlogEntity eachBlog : eachUser
                .getTumblrBlogEntities()) {
                eachBlog.getBlogPostsOffsetEntity().clear();
            }
        }

        @SuppressWarnings("unchecked")
        final List<BlogPostsOffsetEntity> offsets =
        em.createQuery("from " + BlogPostsOffsetEntity.class.getName())
        .getResultList();

        for (final BlogPostsOffsetEntity each : offsets) {
            RemovePrevious.LOG.debug("deleting blog: {}", each);

            em.remove(each);
        }

        em.getTransaction().commit();

        RemovePrevious.LOG.debug("deleted {} offsets",
                                 Integer.valueOf(offsets.size()));

        em.getTransaction().begin();

        @SuppressWarnings("unchecked")
        final List<UserEntity> users2 =
        em.createQuery("from " + UserEntity.class.getName())
        .getResultList();

        for (final UserEntity each : users2) {
            each.getTumblrBlogEntities().clear();
        }

        @SuppressWarnings("unchecked")
        final List<TumblrBlogEntity> blogs =
        em.createQuery("from " + TumblrBlogEntity.class.getName())
        .getResultList();

        for (final TumblrBlogEntity each : blogs) {
            RemovePrevious.LOG.debug("deleting blog: {}", each);

            em.remove(each);
        }

        em.getTransaction().commit();

        RemovePrevious.LOG.debug("deleted {} blogs",
                                 Integer.valueOf(blogs.size()));

        em.getTransaction().begin();

        @SuppressWarnings("unchecked")
        final List<UserEntity> users3 =
        em.createQuery("from " + UserEntity.class.getName())
        .getResultList();

        for (final UserEntity each : users3) {
            RemovePrevious.LOG.debug("deleting user: {}", each);

            em.remove(each);
        }

        RemovePrevious.LOG.debug("deleted {} users",
                                 Integer.valueOf(users3.size()));

        em.getTransaction().commit();
    }

}
